﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.Admin.ProductType
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Utils.CheckAuthentication())
            {
                if (!IsPostBack)
                {
                    LoadData();
                }
            }
        }
        private void LoadData()
        {
            info.InnerHtml = string.Empty;
            if ((string)Session["Info"] != "")
            {
                info.InnerHtml = (string)Session["Info"];
                Session["Info"] = "";
            }

            List<LOAI_SAN_PHAM> dataProductType = LoaiSanPhamModels.GetAll();
            if (dataProductType.Count > 0)
            {
                StringBuilder str = new StringBuilder();
                foreach (var item in dataProductType)
                {
                    string urlUpdate = ResolveClientUrl("~/Admin/ProductType/Update.aspx?ID=" + item.ID);
                    string urlDelete = ResolveClientUrl("~/Admin/ProductType/Delete.aspx?ID=" + item.ID);
                    string urlChangeStatus = ResolveClientUrl("~/Admin/ProductType/ChangeStatus.aspx?ID=" + item.ID + "&Status=" + item.TrangThai);
                    string urlChangeHome = ResolveClientUrl("~/Admin/ProductType/ChangeHome.aspx?ID=" + item.ID + "&Status=" + item.DacBiet);
                    
                    string status = "<a href=\"" + urlChangeStatus + "\" title=\"Hiển thị\" class=\"btn btn-primary btn-circle\"><i class=\"fa fa-check\"></i></a>";
                    if (item.TrangThai == 0)
                        status = "<a href=\"" + urlChangeStatus + "\" title=\"Khóa\" class=\"btn btn-danger btn-circle\"><i class=\"fa fa-lock\"></i></a>";

                    string home = "<a href=\"" + urlChangeHome + "\" title=\"Hiển thị trang chủ\" class=\"btn btn-success btn-circle\"><i class=\"fa fa-home\"></i></a>";
                    if (item.DacBiet == 0)
                        home = "<a href=\"" + urlChangeHome + "\" title=\"Không hiển thị trang chủ\" class=\"btn btn-warning btn-circle\"><i class=\"fa fa-lock\"></i></a>";

                    string urlImage = ResolveClientUrl("~/images/producttype/default.png");
                    if(item.HinhAnh != null)
                        urlImage = ResolveClientUrl("~/images/producttype/"+item.HinhAnh);

                    str.Append("<tr class=\"odd gradePhone\">");
                    str.Append("<td>" + item.TenLoai + "</td>");
                    str.Append("<td>" + LoaiSanPhamModels.GetParentName(item.MaLoaiCha) + "</td>");
                    str.Append("<td><img src=\""+urlImage+"\" style=\"width: 70px;\" /></td>");
                    str.Append("<td class=\"tbitem-status text-center\">" + status + "</td>");
                    str.Append("<td class=\"tbitem-status text-center\">" + home + "</td>");

                    str.Append("<td class=\"tbitem-action text-center\">");
                    str.Append("<a href=\"" + urlUpdate + "\" class=\"btn btn-primary btn-circle\" title=\"Cập nhật\">");
                    str.Append("<i class=\"fa fa-edit\"></i>");
                    str.Append("</a>");
                    str.Append("<button data-toggle=\"modal\" data-target=\"#modalDeleteProductType\" onclick=\"return DeleteProductType('" + LoaiSanPhamModels.CheckDeleteTypeProduct(item.ID) + "','" + urlDelete + "')\" class=\"btn btn-danger btn-circle\" type=\"button\" title=\"Xóa\">");
                    str.Append("<i class=\"fa fa-times\"></i>");
                    str.Append("</button>");
                    str.Append("</td>");
                    str.Append("</tr>");
                }
                lstProductType.InnerHtml = str.ToString();
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/ProductType/Add.aspx");
        }
    }
}